﻿#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)

#include<iostream>
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<graphics.h>
#include<mmsystem.h>
#include<conio.h>
#include<stdbool.h>
#include<easyx.h>

#pragma comment(lib,"winmm.lib")

#define ROW 10
#define COL 10
#define ABC 5

#define SPACE  0  //空地
#define WALL   1  //墙
#define DEST   2  //目的地
#define BOX	   3  //箱子
#define PLAYER 4  //玩家


void ShowMap(int map[][COL])
{
	for (int r = 0; r < ROW; r++)
	{
		for (int c = 0; c < COL; c++)
		{
			//if (map[r][c] == 0)
			//{
			//	printf("  ");
			//}
			//else
			//printf("%d ", map[r][c]);

			switch (map[r][c])
			{
			case SPACE:
				printf("  ");
				break;
			case WALL:
				printf("墙");
				break;
			case DEST:
				printf("☆");
				break;
			case BOX:
				printf("□");
				break;
			case PLAYER:
				printf("♀");
				break;
			case BOX + DEST:   //箱子在目的地上
				printf("★");
				break;
			case PLAYER + DEST://玩家在目的地上
				printf("♂");
				break;
			}
		}
		printf("\n");
	}
}

//推箱子
void PushBox(int map[][COL],int copy[][COL])
{
	//找打玩家所在位置
	int r = -1;
	int c = -1;
	for (int i = 0; i < ROW; i++)
	{
		for (int k = 0; k < COL; k++)
		{
			if (map[i][k] == PLAYER || map[i][k] == DEST + PLAYER)
			{
				r = i;
				c = k;
				goto doneLoop;
			}
		}
	}
doneLoop:

	ExMessage m;
	peekmessage(&m, EX_MOUSE | EX_KEY);

	if (m.message == WM_KEYDOWN)
	{
		if (m.vkcode == 'f' || m.vkcode == 'F')
		{
			for (int i = 0; i < ROW; i++)
			{
				for (int j = 0; j < COL; j++)
				{
					map[i][j] = copy[i][j];
				}
			}
		}
	}
	if (m.message == WM_KEYDOWN)
	{
		if (m.vkcode == 'w' || m.vkcode == 'W' || m.vkcode == 72)
		{
			if (map[r - 1][c] == SPACE || map[r - 1][c] == DEST)
			{
				map[r - 1][c] += PLAYER;
				//2，把玩家从原来的位置减掉
				map[r][c] -= PLAYER;
			}
			//2，玩家的前面是箱子，可以推箱子
			else if (map[r - 1][c] == BOX || map[r - 1][c] == BOX + DEST)
			{
				//判断箱子前面是什么？
				if (map[r - 2][c] == SPACE || map[r - 2][c] == DEST)
				{
					//1，把箱子移动到前面
					map[r - 2][c] += BOX;
					//2，把箱子从原来的位置减掉
					map[r - 1][c] -= BOX;
					//3，把玩家移动到原来箱子的位置
					map[r - 1][c] += PLAYER;
					//4，把玩家从原来的位置减掉
					map[r][c] -= PLAYER;
				}
			}
			mciSendString("play ji.mp3", NULL, 0, NULL);
		}
	}

	if (m.message == WM_KEYDOWN)
	{
		if (m.vkcode == 's' || m.vkcode == 'S' || m.vkcode == 80)
		{
			if (map[r + 1][c] == SPACE || map[r + 1][c] == DEST)
			{
				map[r + 1][c] += PLAYER;
				map[r][c] -= PLAYER;
			}
			else if (map[r + 1][c] == BOX || map[r + 1][c] == BOX + DEST)
			{
				if (map[r + 2][c] == SPACE || map[r + 2][c] == DEST)
				{
					map[r + 2][c] += BOX;
					map[r + 1][c] -= BOX;
					map[r + 1][c] += PLAYER;
					map[r][c] -= PLAYER;
				}
			}
			mciSendString("play ji.mp3", NULL, 0, NULL);
		}
	}

	if (m.message == WM_KEYDOWN)
	{
		if (m.vkcode == 'a' || m.vkcode == 'A' || m.vkcode == 75)
		{
			if (map[r][c - 1] == SPACE || map[r][c - 1] == DEST)
			{
				map[r][c - 1] += PLAYER;
				map[r][c] -= PLAYER;
			}
			else if (map[r][c - 1] == BOX || map[r][c - 1] == BOX + DEST)
			{
				if (map[r][c - 2] == SPACE || map[r][c - 2] == DEST)
				{
					map[r][c - 2] += BOX;
					map[r][c - 1] -= BOX;
					map[r][c - 1] += PLAYER;
					map[r][c] -= PLAYER;
				}
			}
			mciSendString("play ji.mp3", NULL, 0, NULL);

		}
	}

	if (m.message == WM_KEYDOWN)
	{
		if (m.vkcode == 'd' || m.vkcode == 'D' || m.vkcode == 77)
		{
			if (map[r][c + 1] == SPACE || map[r][c + 1] == DEST)
			{
				map[r][c+1] += PLAYER;
				map[r][c] -= PLAYER;
			}
			else if (map[r][c+1] == BOX || map[r][c+1] == BOX + DEST)
			{
				if (map[r][c + 2] == SPACE || map[r][c + 2] == DEST)
				{
					map[r][c + 2] += BOX;
					map[r][c+1] -= BOX;
					map[r][c+1] += PLAYER;
					map[r][c] -= PLAYER;
				}
			}
			mciSendString("play ji.mp3", NULL, 0, NULL);
		}
	}
//	switch (_getch())
//	{
//	case 'w':
//		//什么情况下，玩家能够移动？
////1，玩家的前面是空地、目的地，直直接移动到空地、，目的地
//		if (map[r - 1][c] == SPACE || map[r - 1][c] == DEST)
//		{
//			map[r - 1][c] += PLAYER;
//			//2，把玩家从原来的位置减掉
//			map[r][c] -= PLAYER;
//		}
//		//2，玩家的前面是箱子，可以推箱子
//		else if (map[r - 1][c] == BOX || map[r - 1][c] == BOX + DEST)
//		{
//			//判断箱子前面是什么？
//			if (map[r - 2][c] == SPACE || map[r - 2][c] == DEST)
//			{
//				//1，把箱子移动到前面
//				map[r - 2][c] += BOX;
//				//2，把箱子从原来的位置减掉
//				map[r - 1][c] -= BOX;
//				//3，把玩家移动到原来箱子的位置
//				map[r - 1][c] += PLAYER;
//				//4，把玩家从原来的位置减掉
//				map[r][c] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//
//		break;
//	case 'W':
//		if (map[r - 1][c] == SPACE || map[r - 1][c] == DEST)
//		{
//			map[r - 1][c] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r - 1][c] == BOX || map[r - 1][c] == BOX + DEST)
//		{
//			if (map[r - 2][c] == SPACE || map[r - 2][c] == DEST)
//			{
//				map[r - 2][c] += BOX;
//				map[r - 1][c] -= BOX;
//				map[r - 1][c] += PLAYER;
//				map[r][c] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//
//		break;
//	case 72:
//		if (map[r - 1][c] == SPACE || map[r - 1][c] == DEST)
//		{
//			map[r - 1][c] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r - 1][c] == BOX || map[r - 1][c] == BOX + DEST)
//		{
//			if (map[r - 2][c] == SPACE || map[r - 2][c] == DEST)
//			{
//				map[r - 2][c] += BOX;
//				map[r - 1][c] -= BOX;
//				map[r - 1][c] += PLAYER;
//				map[r][c] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//
//		break;
//	case 's':
//		if (map[r + 1][c] == SPACE || map[r + 1][c] == DEST)
//		{
//			map[r + 1][c] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r + 1][c] == BOX || map[r + 1][c] == BOX + DEST)
//		{
//			if (map[r + 2][c] == SPACE || map[r + 2][c] == DEST)
//			{
//				map[r + 2][c] += BOX;
//				map[r + 1][c] -= BOX;
//				map[r + 1][c] += PLAYER;
//				map[r][c] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//
//		break;
//	case 'S':
//		if (map[r + 1][c] == SPACE || map[r + 1][c] == DEST)
//		{
//			map[r + 1][c] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r + 1][c] == BOX || map[r + 1][c] == BOX + DEST)
//		{
//			if (map[r + 2][c] == SPACE || map[r + 2][c] == DEST)
//			{
//				map[r + 2][c] += BOX;
//				map[r + 1][c] -= BOX;
//				map[r + 1][c] += PLAYER;
//				map[r][c] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//
//		break;
//	case 80:
//		if (map[r + 1][c] == SPACE || map[r + 1][c] == DEST)
//		{
//			map[r + 1][c] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r + 1][c] == BOX || map[r + 1][c] == BOX + DEST)
//		{
//			if (map[r + 2][c] == SPACE || map[r + 2][c] == DEST)
//			{
//				map[r + 2][c] += BOX;
//				map[r + 1][c] -= BOX;
//				map[r + 1][c] += PLAYER;
//				map[r][c] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//
//		break;
//	case 'a':
//		if (map[r][c - 1] == SPACE || map[r][c - 1] == DEST)
//		{
//			map[r][c - 1] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r][c - 1] == BOX || map[r][c - 1] == BOX + DEST)
//		{
//			if (map[r][c - 2] == SPACE || map[r][c - 2] == DEST)
//			{
//				map[r][c - 2] += BOX;
//				map[r][c - 1] -= BOX;
//				map[r][c - 1] += PLAYER;
//				map[r][c - 1] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//
//		break;
//	case 'A':
//		if (map[r][c - 1] == SPACE || map[r][c - 1] == DEST)
//		{
//			map[r][c - 1] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r][c - 1] == BOX || map[r][c - 1] == BOX + DEST)
//		{
//			if (map[r][c - 2] == SPACE || map[r][c - 2] == DEST)
//			{
//				map[r][c - 2] += BOX;
//				map[r][c - 1] -= BOX;
//				map[r][c - 1] += PLAYER;
//				map[r][c - 1] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//
//		break;
//	case 75:
//		if (map[r][c - 1] == SPACE || map[r][c - 1] == DEST)
//		{
//			map[r][c-1] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r][c-1] == BOX || map[r][c-1] == BOX + DEST)
//		{
//			if (map[r][c-2] == SPACE || map[r][c-2] == DEST)
//			{
//				map[r][c - 2] += BOX;
//				map[r][c-1] -= BOX;
//				map[r][c-1] += PLAYER;
//				map[r][c-1] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//
//		break;
//	case 'd':
//		if (map[r][c + 1] == SPACE || map[r][c + 1] == DEST)
//		{
//			map[r][c + 1] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r][c + 1] == BOX || map[r][c + 1] == BOX + DEST)
//		{
//			if (map[r][c + 2] == SPACE || map[r][c + 2] == DEST)
//			{
//				map[r][c + 2] += BOX;
//				map[r][c + 1] -= BOX;
//				map[r][c + 1] += PLAYER;
//				map[r][c + 1] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//		break;
//	case 'D':
//		if (map[r][c + 1] == SPACE || map[r][c + 1] == DEST)
//		{
//			map[r][c + 1] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r][c + 1] == BOX || map[r][c + 1] == BOX + DEST)
//		{
//			if (map[r][c + 2] == SPACE || map[r][c + 2] == DEST)
//			{
//				map[r][c + 2] += BOX;
//				map[r][c + 1] -= BOX;
//				map[r][c + 1] += PLAYER;
//				map[r][c + 1] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//		break;
//	case 77:
//		if (map[r][c + 1] == SPACE || map[r][c + 1] == DEST)
//		{
//			map[r][c+1] += PLAYER;
//			map[r][c] -= PLAYER;
//		}
//		else if (map[r][c+1] == BOX || map[r][c+1] == BOX + DEST)
//		{
//			if (map[r][c + 2] == SPACE || map[r][c + 2] == DEST)
//			{
//				map[r][c + 2] += BOX;
//				map[r][c+1] -= BOX;
//				map[r][c+1] += PLAYER;
//				map[r][c+1] -= PLAYER;
//			}
//		}
//		mciSendString("play ji.mp3", NULL, 0, NULL);
//		break;
//	default:
//		break;
//	}
}

bool ispass(int map[][COL])
{
	for(int i  = 0; i < ROW; i++)
	{
		for(int k = 0; k < COL; k++)
		{
			if( map[i][k] == BOX)
			return false;
		}
	}
	return true;
}


IMAGE imgs[7];
//加载图片资源
void loadReasource()
{
	char file[50] = { 0 };
	for (int i = 0; i < 7; i++)
	{
		sprintf(file, "imag/%d.JPG", i);
		//加载图片
		loadimage(imgs + i, file);
	}
}
//绘制地图
void drawMap(int map[][COL])
{
	for (int i = 0; i < ROW; i++)
	{
		for (int k = 0; k < COL; k++) 
		{
			int x = k * 58;
			int y = i * 58;
			putimage(x, y, imgs + map[i][k]);
		}
	}
}


int main()
{

	//创建窗口
	initgraph(600, 600);//,EX_SHOWCONSOLE
	loadReasource();
	mciSendString("play begin.mp3", NULL, 0, NULL);

	//创建地图
	int map[6][ROW][COL] =
	{
		{
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,1,1,1,0,0,0,0},
		{0,0,0,1,2,1,0,0,0,0},
		{0,0,0,1,3,1,1,1,1,0},
		{0,1,1,1,0,3,0,2,1,0},
		{0,1,2,3,4,0,1,1,1,0},
		{0,1,1,1,1,3,1,0,0,0},
		{0,0,0,0,1,2,1,0,0,0},
		{0,0,0,0,1,1,1,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		},
		{
		{0,0,0,0,1,1,0,0,0,0},
		{0,0,0,1,2,0,1,0,0,0},
		{0,0,1,0,0,0,0,1,0,0},
		{0,1,0,0,3,0,0,0,1,0},
		{1,2,0,3,0,0,0,3,2,1},
		{0,1,0,0,4,0,0,0,1,0},
		{0,0,1,0,0,0,0,1,0,0},
		{0,0,0,1,3,0,1,0,0,0},
		{0,0,0,1,2,0,1,0,0,0},
		{0,0,0,1,1,1,1,0,0,0},
		},
		{
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,1,1,1,1,1,1,0,0,0},
		{0,1,0,0,4,0,1,1,0,0},
		{0,1,0,2,3,2,0,1,1,0},
		{0,1,0,1,3,2,3,0,1,0},
		{0,1,0,0,0,0,0,0,1,0},
		{0,1,1,1,1,1,1,1,1,0},
		},
		{
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,1,1,1,1,0,0,0},
		{0,0,0,1,0,0,1,0,0,0},
		{1,1,1,1,0,0,1,0,0,0},
		{1,0,0,0,0,0,1,0,0,0},
		{1,0,1,1,0,1,1,1,1,0},
		{1,2,2,3,3,4,0,0,1,0},
		{1,1,1,0,0,0,0,0,1,0},
		{0,0,1,1,1,1,1,1,1,0},
		},
		{
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,1,1,1,1,1,0,0,0},
		{0,0,1,2,0,0,1,0,0,0},
		{0,0,1,2,3,4,1,0,0,0},
		{0,0,1,0,3,0,1,0,0,0},
		{0,0,1,0,3,2,1,0,0,0},
		{0,0,1,1,1,1,1,0,0,0},
		},
		{
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,1,1,1,0,0,0,0},
		{0,0,0,1,2,1,0,0,0,0},
		{0,0,0,1,3,1,1,1,1,0},
		{0,1,1,1,0,3,0,2,1,0},
		{0,1,2,3,4,0,1,1,1,0},
		{0,1,1,1,1,3,1,0,0,0},
		{0,0,0,0,1,2,1,0,0,0},
		{0,0,0,0,1,1,1,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		},

	};
	int level = 0;

	int copy[5][ROW][COL] = { 0 };
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			for (int k = 0; k < ABC; k++)
			{
				copy[k][i][j] = map[k][i][j];
			}
		}
	}

	while (1)
	{

		ShowMap(map[level]);
		drawMap(map[level]);
		if (ispass(map[level]))
		{
			level++;
			if (level >= 5)
			{
				printf("!!!!!!!!你过关!!!!!!!!");
				mciSendString("play end.mp3", NULL, 0, NULL);
				system("pause");
				break;
			}
		}

		PushBox(map[level],copy[level]);
		system("cls");
	}
	return 0;
}





